package com.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.config.FileProperties;
import com.constant.AppConstant;
import com.entity.LocalStorage;
import com.entity.SysDeployHistory;
import com.entity.SysVersionManage;
import com.exception.ParamException;
import com.repository.LocalStorageRepository;
import com.repository.SysVersionRepository;
import com.service.FileService;
import com.utils.FileUtil;
import com.vo.FileVo;
import com.vo.VersionVo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Optional;
import java.util.UUID;


/**
 * @author Yadasu
 * @date 2021/1/27 15:31
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class FileServiceImpl implements FileService {

    @Autowired
    private SysVersionRepository sysVersionRepository;
    @Autowired
    private LocalStorageRepository localStorageRepository;

    private final FileProperties properties;


    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer updateFile(FileVo fileVo) {
        return sysVersionRepository.getByAppId(fileVo.getVersion(), fileVo.getVersionId(), fileVo.getUsername());

    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysVersionManage create(MultipartFile multipartFile) {
        FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
        String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
        String type = FileUtil.getFileType(suffix);
        File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + File.separator);
        String abbreviation = file.getName().substring(0, file.getName().indexOf("-"));
        if (ObjectUtil.isNull(file)) {
            throw new ParamException("上传失败");
        }
        try {
            LocalStorage localStorage = new LocalStorage(
                    file.getName(),
                    abbreviation,
                    suffix,
                    file.getPath(),
                    type,
                    FileUtil.getSize(multipartFile.getSize())
            );

            localStorageRepository.save(localStorage);
            //TODO:未能成功判断文件是否存在
            SysVersionManage versionManage = new SysVersionManage();
            versionManage.setPackageName(abbreviation);
            Example<SysVersionManage> example = Example.of(versionManage);
            Optional<SysVersionManage> one = sysVersionRepository.findOne(example);
            if (one.isPresent()) {
                one.get().setFileName(file.getName());
                return sysVersionRepository.save(one.get());
            } else {
                throw new ParamException("请先新建项目");
            }

        } catch (Exception e) {
            FileUtil.del(file);
            throw new ParamException(e.getMessage());
        }
    }
}
